//
//  ZCViewController.m
//  Movie
//
//  Created by cjy on 16/4/8.
//  Copyright © 2016年 YJC. All rights reserved.
//

#import "ZCViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "loginVC.h"
#import <UIImage+GIF.h>

@interface ZCViewController ()
@property (nonatomic, strong) UIImageView *img;
@property (nonatomic, strong) UIImageView *loadingImageView;

@property (nonatomic,strong)UITextField *userText;
@property (nonatomic,strong)UITextField *mimaText;
@property (nonatomic,strong)UITextField *mail;

@property (nonatomic,strong)UIButton *buttonforZc;
@property (nonatomic,strong)UIButton *btnForReturn;

@end

@implementation ZCViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self create];
    [self initLoadingImageView];

}

// 加载动态图片
- (void)initLoadingImageView{
    
    NSString  *name = @"43c353f13e852c1455ce2cd04fa19156.gif";
    
    NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];
    
    NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
    
    
    if (!self.loadingImageView) {
        
        self.loadingImageView = [[UIImageView alloc]init];
        
    }
    
    //    imgView.image = [UIImage animatedImageWithAnimatedGIFData:imageData];
    
    self.loadingImageView.backgroundColor = [UIColor clearColor];
    
    self.loadingImageView.image = [UIImage sd_animatedGIFWithData:imageData];
    
    self.loadingImageView.frame = CGRectMake(80, 80, 100, 80);
    self.loadingImageView.center = CGPointMake(self.view.center.x, self.view.frame.size.height / 3 - 70);
    
    [self.img addSubview:self.loadingImageView];
    
    [self.img bringSubviewToFront:self.loadingImageView];
    
}



- (void)create{
    
    self.img = [[UIImageView alloc] initWithFrame:self.view.frame];
    self.img.image = [UIImage imageNamed:@"h"];
    self.img.userInteractionEnabled = YES;
    
    [self.view addSubview:self.img];

    
    self.userText = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 4 + 20, 10, self.view.frame.size.width / 3 * 2, 35)];
    self.userText.backgroundColor = [UIColor colorWithRed:0 / 255.f green:0 / 255.f blue:0 / 255.f alpha:0.4];
    self.userText.center = CGPointMake(self.view.center.x, self.view.frame.size.height / 3);
    self.userText.textColor = [UIColor whiteColor];
    self.userText.font = [UIFont boldSystemFontOfSize:16];
    self.userText.placeholder = @"请输入用户名: ";
    [self.userText setValue:[UIColor colorWithRed:0.807 green:0.829 blue:0.820 alpha:1.000]  forKeyPath:@"_placeholderLabel.textColor"];
    [self.userText setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    self.userText.layer.cornerRadius = 2.f;
    self.userText.layer.masksToBounds = YES;
    self.userText.keyboardAppearance = UIKeyboardAppearanceDark;
    
    [self.img addSubview:self.userText];
    
    
    self.mimaText = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 4 + 20, 60, self.view.frame.size.width / 3 * 2, 35)];
    self.mimaText.backgroundColor = [UIColor colorWithRed:0 / 255.f green:0 / 255.f blue:0 / 255.f alpha:0.4];
    self.mimaText.center = CGPointMake(self.view.center.x, self.view.frame.size.height / 3 + 45);
    self.mimaText.tintColor = [UIColor whiteColor];
    self.mimaText.placeholder = @"请输入密码: ";     //
    [self.mimaText setValue:[UIColor colorWithRed:0.807 green:0.829 blue:0.820 alpha:1.000]  forKeyPath:@"_placeholderLabel.textColor"];
    [self.mimaText setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    self.mimaText.layer.cornerRadius = 2.f;
    self.mimaText.layer.masksToBounds = YES;
    self.mimaText.hidden = NO;
    self.mimaText.keyboardAppearance = UIKeyboardAppearanceDark;
    self.mimaText.secureTextEntry = YES;
    self.mimaText.textColor = [UIColor whiteColor];
    self.mimaText.font = [UIFont boldSystemFontOfSize:16];

    [self.img addSubview:self.mimaText];
    
    
    self.mail = [[UITextField alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 4 + 20, 120, self.view.frame.size.width / 3 * 2, 35)];
    self.mail.backgroundColor = [UIColor colorWithRed:0 / 255.f green:0 / 255.f blue:0 / 255.f alpha:0.4];
    self.mail.center = CGPointMake(self.view.center.x, self.view.frame.size.height / 3 + 90);
    self.mail.tintColor = [UIColor whiteColor];
    self.mail.placeholder = @"请输入qq邮箱: ";     //
    [self.mail setValue:[UIColor colorWithRed:0.807 green:0.829 blue:0.820 alpha:1.000] forKeyPath:@"_placeholderLabel.textColor"];
    [self.mail setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    self.mail.layer.cornerRadius = 2.f;
    self.mail.layer.masksToBounds = YES;
    self.mail.hidden = NO;
    self.mail.keyboardAppearance = UIKeyboardAppearanceDark;
    self.mail.textColor = [UIColor whiteColor];
    self.mail.font = [UIFont boldSystemFontOfSize:16];
    
    [self.img addSubview:self.mail];

    self.btnForReturn = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 40, 40)];
    [self.btnForReturn setTitle:@"返回" forState:UIControlStateNormal];
    self.btnForReturn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [self.btnForReturn setTitleColor:[UIColor colorWithRed:100 / 255.f green:160 / 255.f blue:155 / 255.f alpha:1] forState:UIControlStateNormal];
    [self.btnForReturn addTarget:self action:@selector(returnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.img addSubview:self.btnForReturn];
    
    self.buttonforZc = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonforZc.frame = CGRectMake(self.view.frame.size.width / 4 + 20, 120, self.view.frame.size.width / 3 + 20, 35);
    self.buttonforZc.center = CGPointMake(self.view.center.x, self.view.frame.size.height / 3 + 170);
    self.buttonforZc.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    self.buttonforZc.layer.borderWidth = 1.0f;    // 外边框
    self.buttonforZc.layer.cornerRadius = 6.f;
    self.buttonforZc.layer.masksToBounds = YES;
    self.buttonforZc.layer.borderColor = [UIColor colorWithRed:0.811 green:0.965 blue:0.876 alpha:1.000].CGColor;
    [self.buttonforZc setTitle:@"确认注册" forState:UIControlStateNormal];
    [self.buttonforZc addTarget:self action:@selector(zc:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.img addSubview:self.buttonforZc];
    
}

- (void)returnAction:(UIButton *)btn{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)zc:(UIButton *)button{

    AVUser *user = [AVUser user];// 新建 AVUser 对象实例
    user.username = self.userText.text;// 设置用户名
    user.password = self.mimaText.text;// 设置密码
    user.email = self.mail.text; // 设置邮箱
    
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            // 注册成功
            
            
            loginVC *login = [[loginVC alloc]init];
            login.userText = self.userText;
            login.mimaText = self.mimaText;
         
            
            [self.navigationController pushViewController:login animated:YES];
            
        } else {
            // 失败的原因可能有多种，常见的是用户名已经存在。
        }
    }];
  
    if (self.userText.text.length < 6 || self.mimaText.text.length < 6 ) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"用户名密码过于简单请重新注册" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:defaultAction];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];

    }else if ([self.mail.text hasPrefix: @"qq.com"])
    {
        
        NSLog(@"dddd");
    
//        [AVUser requestPasswordResetForEmailInBackground:self.mail.text block:^(BOOL succeeded, NSError *error) {
//            if (succeeded) {
//                
//            } else {
//                
//            }
//        }];

    
    
    }else{
    
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"请输入正确的邮箱" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:defaultAction];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];

    
    
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
